<template>
  <GcTable
    v-loading="loading"
    v-model:searchKeywords="searchKeywords"
    :pagination="pagination"
    :columns="CIRCLE_MANAGE_COLLUMNS"
    :tableData="tableData"
    :showFilter="false"
    searchPlaceholder="请输入友圈关键字/所属用户昵称进行搜索"
    @current-change="handlerCurrentChange"
    @search="handlerSearch">
    <template #picList="{ row }">
      <div class="pic-list">
        <el-image
          :z-index="999999"
          preview-teleported
          v-for="(img, idx) in row.picList"
          :key="img"
          style="width: 30px; height: 30px"
          :src="img"
          :zoom-rate="1.2"
          :preview-src-list="row.picList"
          :initial-index="idx"
          fit="cover"
        />
      </div>
    </template>
    <template #operate="{ row }">
      <div class="operate-btn-group center">
        <gc-icon-button
          v-permission="'circle-manage-permission:btn:delete'"
          type="link"
          @click="deleteCircle(row.id)">删除</gc-icon-button>
      </div>
    </template>
  </GcTable>
</template>

<script setup lang="ts" name="ShareCircleManage">
import { ref } from 'vue'
import { CIRCLE_MANAGE_COLLUMNS } from './const'
import type { Circle } from '@/http/type'
import type { Pagination } from '@/@type'
import usePagination from '@/hooks/usePagination'
import { apiGetCircleList, apiDeleteCircle } from '@/http/service'
import { ElMessageBox, ElMessage } from 'element-plus'

interface ResponseCircle extends Pagination {
  dataList: Array<Circle>
}
const { pagination, loading } = usePagination()
const tableData = ref<Array<Circle>>([])
const searchKeywords = ref('')

/**
 * 友圈列表查询
 */
async function findCircleList () {
  const searchParams = {
    pageNo: pagination.pageNo,
    pageSize: pagination.pageSize,
    searchKeywords: searchKeywords.value
  }
  loading.value = true
  try {
    const { dataList, ...pageInfo } = await apiGetCircleList<ResponseCircle>(searchParams)
    tableData.value = dataList
    Object.assign(pagination, pageInfo)
  } catch (error) {
    tableData.value = []
  } finally {
    loading.value = false
  }
}
findCircleList()

async function deleteCircle (id: number) {
  await ElMessageBox.confirm(
    '删除友圈后将无法恢复，确认删除？',
    '提示',
    { type: 'warning' }
  )
  await apiDeleteCircle(id)
  ElMessage.success('删除友圈成功')
  findCircleList()
}

function handlerSearch () {
  pagination.pageNo = 1
  findCircleList()
}

function handlerCurrentChange (val: number) {
  pagination.pageNo = val
  findCircleList()
}
</script>

<style lang="scss" scoped>
.pic-list {
  display: flex;
  justify-content: center;
}
</style>
